<?php

class QpartnersController extends Controller
{
	public $layout = "application.views.layouts.admin";
	public function actionIndex()
	{
		$this->render('index');
	}
	public function actionAdd() 
	{
		$partners=new Partners();
	 if (isset($_POST['ajax'])&& $_POST['ajax']==='news')
	            {
	                # code...
	               
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
	 if (isset($_POST['Partners']))
	 {
	 	
	 	$partners->attributes=$_POST['Partners'];
	 	$partners->image = CUploadedFile::getInstance($partners,'image'); 
	        	$imgas=preg_split('/[.]+/',$partners->image, -1, PREG_SPLIT_NO_EMPTY); 
				$imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));

				
		 if ($partners->validate('reg'))
				{
				
					 
					if ($partners->image!="") {
						$partners->image->saveAs('images/partners/logo/'.$imgas[0].".".$imgas[1]);
						$partners->image=$imgas[0].".".$imgas[1];
					}
						
						
						
					 if($partners->save())
					 {
					 	$this->Resize($partners->image,125,125);
					 	$this->redirect('/admin/qpartners/list');
					 }
				}
	 }
	 $this->render("application.views.qpartners.add",array('partners'=>$partners));
		
	}
	
	public function actionList() 
	{
		$partners=Partners::model()->findAll();
		$this->render("application.views.qpartners.list",array('partners'=>$partners));
	}
	
	public function actionDel() {
			$partners_id = $_GET['id'];			
			$partners_del =Partners::model()->findByPk($partners_id);
			$this->Delete($partners_del->image);
			 
			Partners::model()->deleteByPk($partners_id);
			 
			$this->redirect('/admin/qpartners/list/');
		}
		
		
    public function actionEdit() {
        $partners_id = $_GET['id'];
       
	//
	$partners=Partners::model()->findByPk($partners_id);
    if (isset($_POST['ajax'])&& $_POST['ajax']==='news')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['Partners'])) 
        {
        	
			 $partners->attributes=$_POST['Partners'];			 
			 
        if ($partners->validate('reg'))
			{
				 
				$img= CUploadedFile::getInstance($partners,'image'); 
		
				if($img!=null)
				 	{
				 		$partners_delete = Partners::model()->findByPk($partners_id);
				 		$this->Delete($partners_delete->image);
				 			$partners->image=  CUploadedFile::getInstance($partners,'image'); 		 
				 		
				 		  $imgas=preg_split('/[.]+/',$partners->image, -1, PREG_SPLIT_NO_EMPTY); 
							 $imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));	
				 		$partners->image->saveAs('images/partners/logo/'.$imgas[0].".".$imgas[1]);
				 		$partners->image=$imgas[0].".".$imgas[1];
				 		$this->Resize($partners->image,125,125);
				 		
				 	}
				
				 //$usercv->date=date();

				 if($partners->save())
				 {
				 	$this->redirect('/admin/qpartners/list/');
				 }
			}
        	
        }
       // $news = news::model()->findByPk($news_id);
        
        $Partners = new Partners();
        
        $this->render("application.views.qpartners.edit",array('partners' => $partners, 'Partners'=>$Partners));
    }
	
	
	private function Delete($nameImg) 
		{
			if(@fopen('images/partners/logo/'.$nameImg, "r"))
			{
			 	$imga_del=preg_split('/[.]+/',$nameImg, -1, PREG_SPLIT_NO_EMPTY);
				 		unlink('images/partners/logo/'.$nameImg);
			}
				 		
		}
	private function Resize($nameImage,$with,$height) 
		{
			
			
			Yii::app()->setComponents(array('imagemod'=>array('class'=>'application.extensions.imagemodifier.CImageModifier')));
				 		
				 $imga = Yii::app()->imagemod->load('images/partners/logo/'.$nameImage);
				 $imga->image_resize          = true;
				$imga->image_y         = $height;
				$imga->image_x         = $with;
				//$imga->file_new_name_body = $img->file_src_name;
				$imga->process('images/partners/logo');
				if ($imga->processed) {					
					$imga->clean();
					$imga=preg_split('/[.]+/',$nameImage, -1, PREG_SPLIT_NO_EMPTY); 				
					
					rename('images/partners/logo/'.$imga[0]."_1.".$imga[1],'images/partners/logo/'.$nameImage);
				} else {
					echo 'error : ' . $img->error;
			}
		}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}